package com.jn.gsondemo.Adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.jn.geson_demo.R;
import com.jn.gsondemo.entity.Medal;// 确保导入正确的 Medal 类

import java.util.List;

public class MedalsAdapter extends RecyclerView.Adapter<MedalsAdapter.MedalViewHolder> {

    private final List<Medal> medalList;

    public MedalsAdapter(List<Medal> medalList) {
        this.medalList = medalList;
    }

    @NonNull
    @Override
    public MedalViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_medal, parent, false);
        return new MedalViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull MedalViewHolder holder, int position) {
        Medal medal = medalList.get(position);
        holder.rankTextView.setText(medal.getRank());
        holder.countryNameTextView.setText(medal.getCountryName());
        holder.goldTextView.setText("Gold: " + medal.getGold());
        holder.silverTextView.setText("Silver: " + medal.getSilver());
        holder.bronzeTextView.setText("Bronze: " + medal.getBronze());
    }

    @Override
    public int getItemCount() {
        return medalList.size();
    }

    public static class MedalViewHolder extends RecyclerView.ViewHolder {
        TextView rankTextView;
        TextView countryNameTextView;
        TextView goldTextView;
        TextView silverTextView;
        TextView bronzeTextView;

        public MedalViewHolder(View view) {
            super(view);
            rankTextView = view.findViewById(R.id.rankTextView);
            countryNameTextView = view.findViewById(R.id.countryNameTextView);
            goldTextView = view.findViewById(R.id.goldTextView);
            silverTextView = view.findViewById(R.id.silverTextView);
            bronzeTextView = view.findViewById(R.id.bronzeTextView);
        }
    }
}